package com.threeti.mecool.core.domain.model.plan;

import com.threeti.mecool.core.domain.model.plan.pojo.Plan;
import com.threeti.mecool.core.domain.model.plan.pojo.PlanJob;
import com.threeti.mecool.core.infrastructure.persistence.plan.PlanResultRepository;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configurable
@Entity
@Table(name = "DM_WF_PLAN_RESULT")
public class PlanResult {
    private Long id;
    private Integer version;
    private Plan plan;
    private List<PlanJobResult> planJobResults = new ArrayList<PlanJobResult>();
    private Date finishedAt;
    private PlanResultRepository repository;

    //<editor-fold desc="static find">
    public static PlanResult optByPlanId(Long planId) {
        PlanResult planResult = repository().findByPlan_Id(planId);
        if (planResult == null) {
            planResult = new PlanResult();
            Plan plan = Plan.findById(planId);
            planResult.setPlan(plan);
        }
        return planResult;
    }
    //</editor-fold>

    //<editor-fold desc="find">
    public PlanJobResult optPlanJobResultByPlanJobId(Long planJobId) {
        for (PlanJobResult planJobResult : planJobResults) {
            if (planJobId.equals(planJobResult.getPlanJob().getId())) {
                return planJobResult;
            }
        }
        PlanJobResult planJobResult = new PlanJobResult();
        planJobResult.setPlanResult(this);
        planJobResults.add(planJobResult);
        PlanJob planJob = PlanJob.findById(planJobId);
        planJobResult.setPlanJob(planJob);
        planJobResult.setProjectCode(planJob.getPlanProject().getProjectCode());
        planJobResult.setProjectName(planJob.getPlanProject().getProjectName());
        planJobResult.saveRedundantProperties();
        return planJobResult;
    }
    //</editor-fold>

    @Transactional
    public void save() {
        repository.save(this);
    }

    //<editor-fold desc="get/set">
    private static PlanResultRepository repository() {
        return new PlanResult().repository;
    }

    @Resource
    public void setRepository(PlanResultRepository repository) {
        this.repository = repository;
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    @OneToOne(optional = false)
    public Plan getPlan() {
        return plan;
    }

    @OneToMany(mappedBy = "planResult", cascade = CascadeType.ALL, orphanRemoval = true)
    public List<PlanJobResult> getPlanJobResults() {
        return planJobResults;
    }

    @Temporal(TemporalType.TIMESTAMP)
    public Date getFinishedAt() {
        return finishedAt;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public void setPlan(Plan plan) {
        this.plan = plan;
    }

    public void setPlanJobResults(List<PlanJobResult> planJobResults) {
        this.planJobResults = planJobResults;
    }

    public void setFinishedAt(Date finishedAt) {
        this.finishedAt = finishedAt;
    }
    //</editor-fold>

}
